package com.example.sakurasay.passwordbook.ui

import android.app.Dialog
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import androidx.fragment.app.viewModels
import com.example.sakurasay.databinding.FragmentAddEditIndependentPasswordBinding
import com.example.sakurasay.passwordbook.data.entities.IndependentPasswordEntry
import com.example.sakurasay.passwordbook.viewmodels.IndependentPasswordViewModel
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.google.android.material.bottomsheet.BottomSheetDialogFragment

class AddEditIndependentPasswordSheet : BottomSheetDialogFragment() {

    private var _binding: FragmentAddEditIndependentPasswordBinding? = null
    private val binding get() = _binding!!

    private val viewModel: IndependentPasswordViewModel by viewModels()

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        _binding = FragmentAddEditIndependentPasswordBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val dialog = super.onCreateDialog(savedInstanceState)
        dialog.setOnShowListener {
            val bottomSheetDialog = it as BottomSheetDialog
            val bottomSheet = bottomSheetDialog.findViewById<FrameLayout>(com.google.android.material.R.id.design_bottom_sheet)
            bottomSheet?.let { sheet ->
                val behavior = BottomSheetBehavior.from(sheet)
                behavior.state = BottomSheetBehavior.STATE_EXPANDED
                behavior.skipCollapsed = true
            }
        }
        return dialog
    }


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        binding.buttonSave.setOnClickListener {
            val accountName = binding.editTextAccountName.text.toString()
            val username = binding.editTextUsername.text.toString()
            val password = binding.editTextPassword.text.toString()
            val website = binding.editTextWebsite.text.toString()
            val notes = binding.editTextNotes.text.toString()

            if (accountName.isNotBlank() && username.isNotBlank() && password.isNotBlank()) {
                val entry = IndependentPasswordEntry(
                    accountName = accountName,
                    username = username,
                    password = password, // Remember to encrypt this!
                    website = website,
                    notes = notes,
                    creationDate = System.currentTimeMillis()
                )
                viewModel.insert(entry)
                dismiss()
            }
        }
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }
}